package com.crazy.base.common.utils.excel;

import com.alibaba.excel.EasyExcel;
import com.crazy.base.common.core.exception.BaseException;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.util.List;

@Slf4j
public class ExcelImportUtil {

    private ExcelImportUtil() {}

    public static <T> List<T> doRead(InputStream inputStream, EasyReadListener<T> listener, Class<T> clazz) {
        try {
            EasyExcel.read(inputStream)
                    .sheet(0)
                    .head(clazz)
                    .registerReadListener(listener)
                    .doReadSync();
        } catch (Exception e) {
            log.error("导入数据异常", e);
            throw BaseException.of("导入数据异常");
        }
        return listener.getDataList();
    }
}
